import { createStore } from "vuex";
import axios from "@/plugin/axios";

export default createStore({
  state: {
    user: {},
    allRoute: [], // 用户所有路由权限
    menuList: [], // 左侧菜单渲染数据
    loadingShow: false,
    activeTabIndex: "",
    pageTitle: '',
    areaOption: [],           // 区域下拉框数据
    stationmasterOption: [],  // 站长下拉框数据
    menuData: [
      {
        title: "主页",
        icon: "menu",
        order: "010",
        children: [
          {
            path: "/xytc/home",
            url: '/index',
            menuName: "概览",
          },
        ],
      },

      /* {
        title: "站点",
        icon: "Connection",
        order: "020",
        children: [
          {
            path: "/xytc/site",
            menuName: "站点管理",
          },
          {
            path: "/xytc/stationmaster",
            menuName: "站长管理",
          },
        ],
      }, */

      {
        title: "店铺&菜品",
        icon: "Dish",
        order: "030",
        children: [
          {
            path: "/xytc/store",
            menuName: "店铺管理",
          },
          {
            path: "/xytc/food",
            menuName: "菜品管理",
          },
        ],
      },
      /* {
        title: "订单管理",
        icon: "Tickets",
        order: "050",
        children: [
          {
            path: "/xytc/orderedFood",
            menuName: "菜品汇总",
          },
        ],
      }, */

    ],
  },
  getters: {
    userData(state) {
      return state.user;
    },
  },
  mutations: {
    setUser(state, value) {
      state.user = value;
    },
    setActiveTabIndex(state, value) {
      state.activeTabIndex = value;
    },

  },
  actions: {
    // 区域
    getAreaOptionData(store, param = {forceUpdate:false, okCallback: null}) {
      let {forceUpdate, okCallback} = param;
      if (!store.state.areaOption.length || forceUpdate) {
        axios.get('/jx/jxRegionManage/queryRegions').then(res => {
          if (res.code == 200) {
            res.result.forEach(val => {
              val.name = val.regionName
            })
            store.state.areaOption = res.result;
            okCallback && okCallback(res.result);
          }
        })
      } else {
        okCallback && okCallback(store.state.areaOption);
      }
    },
    // 站长
    getStationmasterOptionData(store, forceUpdate=false) {
      if (!store.state.stationmasterOption.length || forceUpdate) {
        let smData = {keyword: ''}
        axios.get('/jx/jxSiteMasterManage/querySiteMaster', { params: smData }).then(res => {
          if (res.code == 200) {
            store.state.stationmasterOption = res.result
          }
        })
      }
    },

    // 获取用户详情/判断用户登录状态
    getMenuList: (store, routeList) => {

      return new Promise((resolve, reject) => {
        setTimeout(() => {
          let allRoute = [];
          let menuList = [];
          let menuDataList = JSON.parse(JSON.stringify(store.state.menuData)); // 不改变原始数据，防止不同权限的登录异常

          // 匹配url存在的正式存为路由与菜单
          menuDataList.forEach(item => {
            let newChildren = []
            if (item.children) {
              item.children.forEach(child => {
                newChildren.push(child)
                allRoute.push(child.path);
              })
            }
            item.children = newChildren;
            if (newChildren.length > 0) {
              menuList.push(item)
            }
          })
          store.state.allRoute = allRoute;
          store.state.menuList = menuList;
          resolve(1);

        }, 50);
      });
    },

    // 获取用户详情/判断用户登录状态
    getMenuList222: (store, routeList) => {

      return new Promise((resolve, reject) => {
        getMenuList().then(res => {

          let allRoute = [];
          let menuList = [];
          let menuDataList = JSON.parse(JSON.stringify(store.state.menuData)); // 不改变原始数据，防止不同权限的登录异常

          let allUrl = []
          if (res.code == 200) {
            let arr = res.data || [];
            // arr[1].menuList = [arr[1].menuList[1]] // 测试

            // 提取所有url, 用于与child中的匹配
            arr.forEach(val => {
              if (val.menuList instanceof Array) {
                val.menuList.forEach(menu => {
                  allUrl.push(menu.url)
                })
              }
            })

            // 匹配url存在的正式存为路由与菜单
            menuDataList.forEach(item => {
              let newChildren = []
              if (item.children) {
                item.children.forEach(child => {
                  if (allUrl.includes(child.url)) {
                    newChildren.push(child)
                    allRoute.push(child.path);
                  }
                })
              }
              item.children = newChildren;
              if (newChildren.length > 0) {
                menuList.push(item)
              }
            })
            store.state.allRoute = allRoute;
            store.state.menuList = menuList;

            console.log('getmuen 完成');
            resolve(1);
          } else {
            resolve(0);
          }

        }).catch((err) => {
          if (err.message && err.message.includes("timeout")) {
            alert("连接超时，可能网络异常");
          }
          reject();
        });

      });
    },

  },
  modules: {},
});
